package cn.agiledata.bank.common.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.hibernateBean.Account;
import cn.agiledata.bank.hibernateBean.ListAccount;
import cn.agiledata.bank.hibernateBean.SubAccount;
import com.vnv.infrastructure.logic.IPBMgrFactory;

public class TransactionListPage extends PageListTemplate {

	public Object getEntityObj() {
		return new ListAccount();
	}

	private int startpage;

	private long accountId;
	
	private int pageSize;

	Map paraMap = new HashMap();
	public TransactionListPage(int start, long accountId) {
		super();
		this.startpage = start;
		this.accountId = accountId;
		this.pageSize = Constant.PAGESIZE.intValue();
	}
	
	public TransactionListPage(int start,int pageSize, long accountId) {
		super();
		this.startpage = start;
		this.accountId = accountId;
		this.pageSize = pageSize;
	}


	public Map getPara() {

		AccountTemplateInter at = (AccountTemplateInter) IPBMgrFactory
				.getMgr("accountTemplate");
		List ids = new ArrayList();

		Account LoginAccount = at.getAccount(String.valueOf(accountId));
		if (LoginAccount != null) {
			Iterator iter = LoginAccount.getSubAccounts().iterator();
			while (iter.hasNext()) {
				ids.add(((SubAccount) iter.next()).getId());
			}
		}
		if (ids.size() > 0)
			paraMap.put("id", ids);

		// TODO Auto-generated method stub
		List statusList = new ArrayList();
		statusList.add(Constant.TRANS_STATUS_INIT);
		statusList.add(Constant.TRANS_STATUS_WAITFORBUYER);
		statusList.add(Constant.TRANS_STATUS_WAITFORBUYERAFFIRM);
		statusList.add(Constant.TRANS_STATUS_WAITFORBUYERPAY);
		statusList.add(Constant.TRANS_STATUS_APPLYREFUND);
		statusList.add(Constant.TRANS_STATUS_DISPUTED);
		paraMap.put("status", statusList);
		paraMap.put("actiontype", Constant.ACTION_TYPE_C);

		/*
		 * ReportForm reportform = (ReportForm)form; if
		 * (!StringUtils.isNotEmpty(reportform.getBegindate())){
		 * paraMap.put("startdate", DateUtil.string2Date("1900-01-01")); }else{
		 * paraMap.put("startdate", reportform.getBegindate()); } if
		 * (!StringUtils.isNotEmpty(reportform.getEnddate())){
		 * paraMap.put("enddate", new Date()); }else{ paraMap.put("enddate",
		 * DateUtil.string2Date(reportform.getEnddate())); }
		 */
		return paraMap;
	}

	public String getSqlStr() {
		StringBuffer hiSql = new StringBuffer(
				"select v_t.trans_id as {ListAccount.tranId}"
                        + ",v_t.order_id as {ListAccount.orderId}"
                        + ",v_t.order_url as {ListAccount.orderUrl}"
						+ ",v_t.status as {ListAccount.status}"
						+ ",to_char(v_t.begin_date,'yyyy-MM-dd HH24:MI:SS') as {ListAccount.beginDate}"
						+ ",to_char(v_t.update_time,'yyyy-MM-dd HH24:MI:SS') as {ListAccount.updateDate}"
						+ ",v_t.amount as {ListAccount.amount}"
						+ ",v_t.trade as {ListAccount.trade}"
						+ ",v_t.id as {ListAccount.id}"
						+ ",acc.name as {ListAccount.name}"
						+ ",acc.email as {ListAccount.email}"
						+ ",acc.mobile as {ListAccount.mobile}"
						+ ",sub_1.account_name as {ListAccount.selfName}"
						+ ",v_t.comeorgo as {ListAccount.comeOrGo}"
						+ ",v_t.trans_status  as {ListAccount.tranStatus}"
						+ ",v_t.RELATING_SEQ  as {ListAccount.relating}"
						+ ",acc.id as {ListAccount.accid}"
						+ ",null  as {ListAccount.remark}"
						+ ",null  as {ListAccount.nameid}"
						+ ",null  as {ListAccount.selfid}"
						+ " from ( "
						+ "		select trans.RELATING_SEQ,trans.order_id,trans.order_url,trans.trans_id,sb.list_name as status,trans.begin_date,trans.update_time,trans.amount,sb1.list_name as trade,trans.id, "
						+ "		case when trans.debit_seq in (:id) then trans.credit_seq "
						+ "		when trans.credit_seq in (:id) then trans.debit_seq end as offsize "
						+ "		,case when trans.debit_seq in (:id) then '0' "
						+ // 付款方
						"		when trans.credit_seq in (:id) then '1' end as comeorgo,trans.trans_status, "
						+ "		case when trans.debit_seq in (:id) then trans.debit_seq "
						+ "		when trans.credit_seq in (:id) then trans.credit_seq end as selfsize "
						+ "		from transaction trans   "
						+ // 收款方

						"		left join sysbook sb on "
						+ "			sb.type_id='TRANS_STATUS' and trans.trans_status = sb.list_id  "
						+ "		left join sysbook sb1 on "
						+ "			sb1.type_id='TRADE_TYPE' and trans.trade_type= sb1.list_id "
						+

						"		where trans.action_seq = :actiontype  "
						+ "		and trans.trans_status in (:status)  ");
		if (getPara().get("actiontype").equals(Constant.ACTION_TYPE_B)) {
			hiSql
					.append("			and trans.begin_date>:startdate and trans.begin_date <:enddate ");
		}

		hiSql
				.append("			and (trans.debit_seq in (:id) or trans.credit_seq in (:id)) "
						+ "		) v_t "
						+ "		left join sub_account sub_1 on "
						+ "			sub_1.id = v_t.selfsize  "
						+ "			,account acc,sub_account sa "
						+ "		where sa.main_account_seq = acc.id and sa.id = v_t.offsize  "
						+ "		order by v_t.id desc,v_t.begin_date desc");
		// TODO Auto-generated method stub
		return hiSql.toString();
	}

	public int getStartPage() {
		// TODO Auto-generated method stub
		return startpage;
	}

	public int getPageSize() {
		return pageSize;
	}




}
